﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccess
{
    public class RolesRepository: ConnectionClass
    {
        public RolesRepository() : base() { }

        public RolesRepository(bool isAdmin)
            : base(isAdmin)
        {
        }

        public Role GetRole(int roleID)
        {
            return Entity.Roles.SingleOrDefault(r => r.RoleID == roleID);
        }

        public void AllocateRole(User user, Role role)
        {
            user.Roles.Add(role);
            Entity.SaveChanges();
        }

        public bool IsUserInRole(string username, int roleId)
        {
            User u = new UsersRepository().GetUser(username);
            if (u.Roles.SingleOrDefault(r => r.RoleID == roleId) == null)

                return false;
            else return true;
        }
    }
}
